/*
 * Copyright (C) 2012 SeekDeepWithin.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package com.seekdeepwithin.DataAccess;

import java.util.*;
import javafx.beans.property.*;
import javafx.concurrent.*;
import com.seekdeepwithin.Data.Book;
import com.seekdeepwithin.Data.ReadLink;

/**
 * User: Jonathan Montiverdi
 * Date: Oct 22, 2012
 *
 * Finds a list
 */
public class PassageFinderService extends Service<String>
{
    private ObjectProperty<List<ReadLink>> linkList = new SimpleObjectProperty <> ();

    /**
     * Sets the list of links to find.
     * @param links The list of links to find.
     */
    public final void setLinkList (List<ReadLink> links)
    {
        this.linkList.set (links);
    }

    /**
     * Gets the list of links to find.
     * @return The list of links to find.
     */
    public final List<ReadLink> getLinkList ()
    {
        return this.linkList.get ();
    }

    /**
     * Gets the list of links property.
     * @return List of links property.
     */
    public final ObjectProperty<List<ReadLink>> linkListProperty ()
    {
        return this.linkList;
    }
    /**
     * Creates the task that performs the action.
     * @return Newly created task.
     */
    @Override
    protected Task<String> createTask ()
    {
        final List<ReadLink> links = getLinkList ();
        return new Task<String> () {
            @Override protected String call ()
            {
                StringBuilder html = new StringBuilder ("<table style=\"font-size:small;font-family:Arial,sans-serif\">");
                Map <String, List<ReadLink>> books = new HashMap<> ();
                for (ReadLink link : links)
                {
                    if (books.containsKey (link.getBook ()))
                    {
                        books.get (link.getBook ()).add (link);
                    }
                    else
                    {
                        List<ReadLink> newLinks = new ArrayList<> ();
                        newLinks.add (link);
                        books.put (link.getBook (), newLinks);
                    }
                }

                for (Map.Entry<String, List<ReadLink>> entry : books.entrySet ())
                {
                    String bookName = entry.getKey ();
                    List<ReadLink> linkList = entry.getValue ();
                    Book book = SdwDatabase.getInstance ().getBook (bookName);
                    if (book == null)
                    {
                        html.append ("<tr><td colspan=\"2\"><h4>You do not have '")
                                .append (bookName)
                                .append ("' downloaded. Please visit the library to download it.</h4></td></tr>");
                    }
                    else
                    {
                        html.append ("<tr><td colspan=\"2\"><h4 style=\"margin:0px;padding:0px;\">")
                                .append (bookName)
                                .append ("</h4></td></tr>");
                        for (ReadLink link : linkList)
                        {
                        }
                    }
                }
                html.append ("</table>");
                return html.toString ();
            }
        };
    }

}
